package minecrafter.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.Thread;

public class Streams
{
  public static int pipeStreams(InputStream in, OutputStream out)
    throws IOException
  {
    byte[] b = new byte[8192];

    int total = 0;
    int read = 0;
    
    while (true)
    {
      try {
        read = in.read(b);
        if (read != -1)
          break;
      }
      catch (IOException e)
      {
        break;
      }
      out.write(b, 0, read);
      total += read;
    }
    
    out.flush();
    return total;
  }

  public static void pipeStreamsActive(InputStream in, OutputStream out) {
    PipeThread thread = new PipeThread(in, out);
    thread.start();
  }
}

class PipeThread extends Thread {
      InputStream in;
      OutputStream out;
      
      PipeThread(InputStream in, OutputStream out) {
        this.in = in;
        this.out = out;
      }
      
      public void run() {
        byte[] b = new byte[8192];
        try
        {
          while (true)
          {
            int count = this.in.read(b);
            if (count == -1) return;
            this.out.write(b, 0, count);
            this.out.flush();
          }
        }
        catch (IOException e)
        {
        }
      }
    }
